package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 业务逻辑实现类
 */

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper ,AdChannel> implements  AdChannelService{


    /**
     * 查询所有数据，和按名字查询
     * @param dto
     * @return
     */
    @Override
    public PageResultVo<AdChannel> findPage(ChannelPageRequestDto dto) {

        //1.构建分页对象
        IPage<AdChannel> page1 = new Page(dto.getPage(),dto.getSize());

        //2.构建查询对象
        LambdaQueryWrapper<AdChannel> lwq = Wrappers.<AdChannel>lambdaQuery()

        //3.追加=查询条件
        //3.1模糊查询   p1 = ture 时才执行查询
        .like(StringUtils.isNotEmpty(dto.getName()),AdChannel::getName,dto.getName())
        //3.2条件查询
        .eq(null!=dto.getStatus(),AdChannel::getStatus,dto.getStatus());

        //3.3 分页查询
         page(page1,lwq);

        //返回数据
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());
    }


    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @Override
    public void channelSave(AdChannel adChannel) {


        //1.添加字段
        adChannel.setAsDefault(false);
        adChannel.setCreatedTime( new Date());

        //2.添加数据
        save(adChannel);
    }


}
